<?php
class User extends Model {
	
	private static $table_name = 'ELEVE';
	
	public static function tryLogin($login, $pass) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("SELECT * FROM ".self::$table_name." WHERE ADRESSEMAIL=:mail AND PASSWORD=:pass");
		$requete->execute(array('mail' => $login,'pass'=> $pass));
		$tab = $requete->fetch(PDO::FETCH_BOTH);
		return new User($tab);
	}
	
	public static function getUserInfos($id) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("SELECT * FROM ".self::$table_name." WHERE IDELEVE=:id");
		$requete->execute(array('id' => $id));
		$tab = $requete->fetch(PDO::FETCH_BOTH);
		return new User($tab);
	}
	
	public static function getAllUsers($me) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT * FROM ".self::$table_name." 
											WHERE IDELEVE!=:idMe
											ORDER BY PSEUDO ASC");
		$requete->execute(array('idMe'=>$me));
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_BOTH)) {
			$data[$i]= new User($tab);
			$i++;
		}
		return $data;
	}
	
	public function __construct($userData) {
		parent::__construct();
		$this->values = new stdClass;
		$this->values = $userData;
	}
	
	public function getId() { return $this->values['IDELEVE'];}
	public function getMailAdress() { return $this->values['ADRESSEMAIL'];}
	public function getPassword() { return $this->values['PASSWORD'];}
	public function getPseudo(){ return $this->values['PSEUDO'];}
	public function getNom() { return $this->values['NOM'];}
	public function getPrenom() { return $this->values['PRENOM'];}
	public function getStatusId() { return $this->values['IDSTATUT'];}
	public function getIdFormation() { return $this->values['IDFORMATION'];}
	public function getAnnee() { return $this->values['ANNEEPROMO'];}
	public function getBirthDate() { return $this->values['DATENAISSANCE'];}
	public function getPictureFilename() { return (__PHOTOS_PATH.$this->values['IDELEVE'].'.jpg');}
	
	
	public function getStatutName(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("SELECT NOMSTATUT FROM STATUT WHERE IDSTATUT = :idstatut");
		$requete->execute(array('idstatut'=>$this->getStatusId()));
		$tab = $requete->fetch(PDO::FETCH_BOTH);
		return $tab['NOMSTATUT'];
	}
	
	public function getConnectedUsers(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("SELECT DISTINCT(IDELEVE) FROM CONNEXION WHERE DATEDECONNEXION IS NULL");
		$requete->execute();
		$i=0;
		$data = array();
		while($tab = $requete->fetch(PDO::FETCH_BOTH)) {
			$data[$i]= User::getUserInfos($tab['IDELEVE']);
			$i++;
		}
		return $data;
	}
	
	public static function createUser($nom,$prenom,$pseudo,$password,$adressemail,$anneepromo=NULL, $formation=1, $datenaissance=NULL){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	INSERT INTO ELEVE(NOM,PRENOM,PSEUDO,PASSWORD,ADRESSEMAIL,ANNEEPROMO, IDFORMATION,DATENAISSANCE,IDSTATUT)
											VALUES(:nom,:prenom,:pseudo,:password,:adressemail,:anneepromo,:formation,:datenaissance,2)");
		$requete->execute(array('nom'=>$nom,'prenom'=>$prenom,'pseudo'=>$pseudo,'password'=>$password,'adressemail'=>$adressemail,
									'anneepromo'=>$anneepromo,'formation'=>$formation,'datenaissance'=>$datenaissance));
	}
	
	public static function checkMail($mail) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("SELECT ADRESSEMAIL FROM ELEVE WHERE ADRESSEMAIL=:mail");
		$requete->execute(array('mail'=>$mail));
		if(($tab = $requete->fetch(PDO::FETCH_ASSOC))=='')
			return 'ok';
		else 
			return 'none';
	}
	
	public static function checkPseudo($pseudo) {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("SELECT PSEUDO FROM ELEVE WHERE PSEUDO=:pseudo");
		$requete->execute(array('pseudo'=>$pseudo));
		if(($tab = $requete->fetch(PDO::FETCH_ASSOC))=='')
			return 'ok';
		else 
			return 'none';
	}
	
	public static function getFormations() {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT IDFORMATION, LIBFORMATION
											FROM FORMATION
										");
		$requete->execute();
		$tab = $requete->fetchAll(PDO::FETCH_ASSOC);
		return $tab;
	}
	
	public function getFormation() {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT LIBFORMATION 
											FROM FORMATION 
											WHERE IDFORMATION=:id
										");
		$requete->execute(array('id'=> $this->getIdFormation()));
		$tab = $requete->fetch(PDO::FETCH_ASSOC);
		return $tab;
	}
	
	public function connexion(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	INSERT INTO CONNEXION(IDELEVE,DATECONNEXION)
											VALUES(:id,NOW())
										");
		$requete->execute(array('id'=>$this->getId()));
	}
	public function deconnexion(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE CONNEXION
											SET DATEDECONNEXION = NOW()
											WHERE IDELEVE = :id AND DATEDECONNEXION IS NULL
										");
		$requete->execute(array('id'=>$this->getId()));
	}
	
	// Requètes modification profil
	public function modifierPseudo($pseudo){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE ELEVE
											SET PSEUDO =:pseudo
											WHERE IDELEVE =:id
										");
		$requete->execute(array('pseudo'=>$pseudo,'id'=>$this->getId()));
	}
	
	public function modifierPass($pass){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE ELEVE
											SET PASSWORD = :pass
											WHERE IDELEVE = :id
										");
		$requete->execute(array('pass'=>md5($pass),'id'=>$this->getId()));
	}
	
	public function modifierPromo($promo){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE ELEVE
											SET ANNEEPROMO = :promo
											WHERE IDELEVE = :id
										");
		$requete->execute(array('promo'=>$promo,'id'=>$this->getId()));
	}
	public function modifierStatut($statut){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE ELEVE
											SET IDSTATUT = :statut
											WHERE IDELEVE = :id
										");
		$requete->execute(array('statut'=>$statut,'id'=>$this->getId()));
	}
	public function modifierFormation($formation){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE ELEVE
											SET IDFORMATION = :formation
											WHERE IDELEVE = :id
										");
		$requete->execute(array('formation'=>$formation,'id'=>$this->getId()));
	}
	public function modifierDateNaissance($date){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	UPDATE ELEVE
											SET DATENAISSANCE = :date
											WHERE IDELEVE = :id
										");
		$requete->execute(array('date'=>$date,'id'=>$this->getId()));
	}
	// Créer une salle
	public function creerSalle($nomsalle,$themes,$idgroupes,$message){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	INSERT INTO SALLE(IDELEVE,NOMSALLE,DATECREATION)
											VALUES (:user,:nomsalle,NOW())");
		$requete->execute(array('user'=>$this->getId(),'nomsalle'=>$nomsalle));
		$salle = Loader::loadModel("Salle");
		$IDSalle = $dbConnection->lastInsertId();
		$salle = Salle::getSalle($IDSalle);
		Loader::loadModel("Groupe");
		$groupes = array();
		$i = 0;
		foreach($idgroupes as $idgroupe){
			$groupes[$i] = Groupe::getGroupe($idgroupe);
			$i++;
		}
		$salle->inviter($groupes);
		$salle->attribuerTheme($themes);
		
		Loader::loadModel('Message');
		Message::creerMessage($salle->getId(), $this->getId(), $salle->getNom(), $message);
	}
	
	public function modifierSalle($idsalle, $nomsalle,$themes,$idgroupes){
		$salle = Loader::loadModel("Salle");
		$salle = Salle::getSalle($idsalle);
		$salle->modifierTitreSalle($nomsalle);
		Loader::loadModel("Groupe");
		$groupes = array();
		$i = 0;
		foreach($idgroupes as $idgroupe){
			$groupes[$i] = Groupe::getGroupe($idgroupe);
			$i++;
		}
		$salle->inviter($groupes);
		$salle->attribuerTheme($themes);
	}
	
	public function getUnreadConversations() {
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT COUNT(DISTINCT(IDELEVE)) 
											FROM MESSAGE_PRIVE 
											WHERE IDELEVE_DEST=:id 
											AND EST_LU=0 
											GROUP BY IDELEVE
											"
										);
		$requete->execute(array('id'=>$this->getId()));
		$data = $requete->fetchAll(PDO::FETCH_ASSOC);
		$number=0;
		foreach($data as $nb)
			$number=$number+$nb['COUNT(DISTINCT(IDELEVE))'];
		return $number;
	}
	// Pour les stats
	public function getNumberOfUsers(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT COUNT(DISTINCT(IDELEVE)) 
											FROM ELEVE
										");
		$requete->execute();
		$data = $requete->fetch(PDO::FETCH_ASSOC);
		return $data['COUNT(DISTINCT(IDELEVE))'];
	}
	public function getNumberOfConnectedUsers(){
		$dbConnection = PDOConnection::getInstance();
		$requete=$dbConnection->prepare("	SELECT COUNT(DISTINCT(IDELEVE)) 
											FROM CONNEXION
											WHERE DATEDECONNEXION IS NULL
										");
		$requete->execute();
		$data = $requete->fetch(PDO::FETCH_ASSOC);
		return $data['COUNT(DISTINCT(IDELEVE))'];
	}

	
}